/*
Created By VietRise on 11/6/2014
*/

#include "Diamond.h"
#include "Scenes\ScreenGameplay.h"

CCAction* Diamond::_appear = NULL;
CCAction* Diamond::_disappear = NULL;
CCAction* Diamond::_rotate = NULL;
CCAction* Diamond::_scale = NULL;
CCAction* Diamond::_fadeOut = NULL;

Diamond::~Diamond()
{
	CC_SAFE_RELEASE(_appear);
	CC_SAFE_RELEASE(_disappear);
	CC_SAFE_RELEASE(_rotate);
	CC_SAFE_RELEASE(_scale);
	CC_SAFE_RELEASE(_fadeOut);
}

Diamond::Diamond(ScreenGameplay* game)
{
	_game = game;
	_canHit = false;
	_atHole = -1;
	_timeAppear = 0;
	_timeDelay = 0;
	_countTimeDelay = 0;
	this->initWithSpriteFrameName("obj_diamond_05.png");
	this->setVisible(false);
}

Diamond* Diamond::create(ScreenGameplay* game)
{
	Diamond* sprite = new Diamond(game);
	if (sprite)
	{
		sprite->autorelease();
		return sprite;
	}

	CC_SAFE_DELETE(sprite);
	return NULL;
}

void Diamond::createAction()
{
	// Action Appear
	CCAnimation* animation;
	animation = CCAnimation::create();
	CCSpriteFrame* frame;
	for (int i = 1; i <= 5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "obj_diamond_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}

	animation->setDelayPerUnit(TIME_ANIMATION_DIAMOND / 5);
	animation->setRestoreOriginalFrame(true);	
	_appear = CCAnimate::create(animation);
	_appear->retain();

	// Action Disappear
	_disappear = CCDelayTime::create(TIME_DISAPPEAR_DIAMOND);
	_disappear->retain();

	// Action EffectWasHit
	_rotate = CCRepeatForever::create(CCRotateBy::create(TIME_EFFECT_DIAMOND/4, -90));
	_rotate->retain();

	_scale = CCScaleTo::create(TIME_EFFECT_DIAMOND, 0.1f);
	_scale->retain();

	_fadeOut = CCFadeOut::create(1.0f);
	_fadeOut->retain();		
}

void Diamond::appear(int atHole)
{
	this->setVisible(true);
	_canHit = true;
	_atHole = atHole;

	this->setRotation(0);
	this->setOpacity(255);
	this->setScale(1);

	this->stopAllActions();
	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCAnimate*)Diamond::_appear->copy()->autorelease(),
		CCDelayTime::create(_timeAppear - TIME_ANIMATION_DIAMOND),
		CCCallFuncN::create(this, callfuncN_selector(Diamond::animationAppearDone)),
		NULL);

	this->runAction(sequence);
}

void Diamond::effectWasHit(CCPoint pos)
{
	_canHit = false;
	this->stopAllActions();

	this->runAction((CCAction*)_rotate->copy()->autorelease());
	this->runAction((CCAction*)_scale->copy()->autorelease());
	this->runAction((CCAction*)_fadeOut->copy()->autorelease());


	CCFiniteTimeAction* sequence = CCSequence::create(
		CCMoveTo::create(TIME_EFFECT_DIAMOND, pos),
		CCCallFuncN::create(this, callfuncN_selector(Diamond::diamondHide)),
		NULL);
	
	this->runAction(sequence);
}

void Diamond::animationAppearDone(CCNode* pSender)
{
	_canHit = false;

	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_diamond_06.png"));
	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCDelayTime*)_disappear->copy()->autorelease(),
		CCCallFuncN::create(this, callfuncN_selector(Diamond::diamondHide)),
		NULL);
	this->runAction(sequence);
}


void Diamond::diamondHide(CCNode* pSender)
{
	pSender->setVisible(false);
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_diamond_05.png"));
	_game->setHoleBlank(_atHole);
	_atHole = -1;	
}

float Diamond::randomFloat(float a, float b)
{
	float random = CCRANDOM_0_1();
	float diff = b - a;
	float r = random * diff;
	return a + r;
}

void Diamond::initTimeDelay(float timeDelay1, float timeDelay2)
{
	_timeDelay = randomFloat(timeDelay1, timeDelay2);
}

void Diamond::initTimeAppear(float timeAppear1, float timeAppear2)
{
	_timeAppear = randomFloat(timeAppear1, timeAppear2);
}
